<?php
// +----------------------------------------------------------------------
// | THINKPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: xlomy <hawk541501@gmail.com> 
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author xlomy <hawk541501@gmail.com> 
 */
class WeiboController extends AdminController {

    /**
     * 首页
     * @return none
     */
    public function index($uid=0, $username=''){

        $where = '1=1';
        if($is_seller = session('is_seller')){   //卖家
            $where .= ' and status=1';
        }
        //是否根据用户id查看用户所有微博
        if($uid!=0 && is_numeric($uid)){
            $where .= ' and uid='.$uid;
        }
        if(!empty($username)){
            $where .= ' and username like "%' .$username. '%"';
        }
        $page = page('Weibo', 10, $where, array('md_weibo.id' =>'desc'), 'md_user on md_weibo.uid=md_user.id', 'md_weibo.id as wid, username, content, add_time, md_weibo.status');
        $page['is_seller'] = $is_seller;
        $data = $page['list'];
        foreach ($data as $k => $v){
            //发布人用户名获取
            $data[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);    //时间处理
            $data[$k]['pics'] = M('Weibo_pic')->where(array('w_id' => $v['wid']))->select(); //获取该微博下所有图片
        }
        $page['list'] = $data;
        $this->assign($page);
        $this->display();

    }
    
    /**
     * 审核微博
     * @param int $wid 微博id
     * @return json 审核是否成功
     */
    public function checkWeibo($wid=0, $status=1){
        if(M('Weibo')->save(array('id' => $wid, 'status' => $status))){
            $this->ajaxReturn(array('code' => 1));
        }
        $this->ajaxReturn(array('code' => 0));
    }

    /**
     * 删除微博
     * @param type $wid 微博id
     */
    public function delWeibo($wid=0){
        $weibo = M('Weibo_pic')->where(array('w_id' => $wid))->select();
        foreach ($weibo as $key => $value) {
            unlink('.' . $value['pic_url']);
        }
        M('Weibo')->delete($wid);
        $this->redirect('Admin/Weibo/index');
    }
}